/*
 * This file is part of VLCJ.
 *
 * VLCJ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VLCJ is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VLCJ.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright 2009, 2010, 2011 Caprica Software Limited.
 */

package uk.co.caprica.vlcj.dbus;

import org.freedesktop.dbus.DBusInterface;
import org.freedesktop.dbus.exceptions.DBusException;

/**
 * Base implementation for a proxy class that wraps a DBus service interface.
 */
public abstract class AbstractDBusInterface {

  /**
   * Connection session.
   */
  private final DBusSession dbusSession;
  
  /**
   * Object path.
   */
  private final String objectPath;
  
  /**
   * Service name.
   */
  private final String serviceBusName;
  
  /**
   * Create an interface wrapper.
   * 
   * @param dbusSession session
   * @param objectPath object path
   * @param serviceBusName service name
   */
  protected AbstractDBusInterface(DBusSession dbusSession, String objectPath, String serviceBusName) {
    this.dbusSession = dbusSession;
    this.objectPath = objectPath;
    this.serviceBusName = serviceBusName;
  }

  /**
   * Get a proxy to the DBus service.
   * 
   * @param <T> type of service
   * @param type type of service
   * @return service
   * @throws DBusServiceNotAvailableException if the service could not be obtained
   */
  protected final <T extends DBusInterface> T service(Class<T> type) throws DBusServiceNotAvailableException {
    try {
      return dbusSession.connection().getRemoteObject(serviceBusName, objectPath, type);
    }
    catch(DBusException e) {
      throw new DBusServiceNotAvailableException(e);
    }
  }
}
